package Desort;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

public class FirstIndexMapper extends Mapper<LongWritable,Text,Text, IntWritable> {
    /*
    * setup：在map之前进行相关变量或者集中资源初始化工作
    *context：上下文对象
    * */
    String name;
    Text  k = new Text();
    IntWritable v = new IntWritable();
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
       //获取文件名a.txt
       FileSplit split = (FileSplit) context.getInputSplit();
       name = split.getPath().getName();
    }

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
       //获取一行数据yyb Tom_255 ping_588
        String line = value.toString();
        //数据切分
        String[] fields = line.split(" ");
        //遍历切分的字段
        for(String keyword:fields){
            //封装数据
            k.set(keyword+"--"+name);
            v.set(1);
            //写出数据
            context.write(k,v);
        }

    }
}
